package cn.lysj.framework.coupon.controller;

import cn.lysj.framework.base.BaseController;
import cn.lysj.framework.common.annotation.CurrentUser;
import cn.lysj.framework.common.domain.User;
import cn.lysj.framework.common.utils.pagination.PageVo;
import cn.lysj.framework.common.utils.web.ParamUtil;
import cn.lysj.framework.common.utils.web.Resp;
import cn.lysj.framework.common.validate.annotation.Validator;
import cn.lysj.framework.coupon.domain.Coupon;
import cn.lysj.framework.coupon.service.CouponService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;

/**
 * @author zwj
 * @description 优惠券控制层
 * @create 2019-01-16 09:38:37
 **/
@Slf4j
@RestController
@Api(description = "优惠券管理接口")
@RequestMapping("/coupon")
public class CouponController extends BaseController<Coupon>{

    @Resource
    private CouponService couponService;

    @Override
    public CouponService getService() {
        return couponService;
    }

    @Override
    public Resp save(Coupon entity){
        return null;
    }

    @Override
    public Resp list(Coupon entity, PageVo pageVo){
        return null;
    }
    @Override
    public Resp update(Coupon entity){
        return null;
    }
    @Override
    public Resp delete(String[] ids){
        return null;
    }

    @PostMapping("/save_rewrite")
    public Resp save_rewrite(@CurrentUser User user, Coupon entity){
        return couponService.storeBuildCoupon(user.getStoreId(),entity);
    }

    @PostMapping("/update_rewrite")
    public Resp updateRewrite(@CurrentUser User user, Coupon entity){
        if(ParamUtil.isBlank(entity.getId())){
            return new Resp(Resp.PARAM_ERROR,"参数错误!");
        }
        Coupon coupon = couponService.getRepository().findOne(entity.getId());
        if(coupon == null){
            return new Resp(Resp.PARAM_ERROR,"非法操作!");
        }
        if(ParamUtil.isBlank(entity.getEventImg())){
            entity.setEventImg(coupon.getEventImg());
        }
        entity.setQrCode(coupon.getQrCode());
        entity.setCreateTime(coupon.getCreateTime());
        entity.setUpdateTime(coupon.getUpdateTime());
        entity.setStoreId(coupon.getStoreId());
        Resp resp = couponService.storeBuildCoupon(entity.getStoreId(),entity);
        if(resp.getCode() != 200){
            return resp;
        }
        return new Resp("修改优惠券操作成功!");
    }

    @GetMapping("/list_rewrite")
    public Resp list(@CurrentUser User user,Coupon entity, PageVo pageVo){
        entity.setDelFlag(1);
        entity.setStoreId(user.getStoreId());
        return new Resp(getService().list(entity,pageVo),"获取优惠券列表成功!");
    }

    /**
     * @author ZhangWenJian
     * @date 2019/1/21 14:43
     * @title birthdayCanAddCoupon
     * @param user
     * @return cn.lysj.framework.common.utils.web.Resp
     * @description 可添加的优惠券
     */
    @PostMapping("/select_coupon_to_use_config")
    public Resp selectCouponToUseConfig(@CurrentUser User user,String[] couponIds){
        return couponService.selectCouponVoToUseConfig(couponIds,user.getStoreId());
    }
}
